/**
 * \file Queue.cc
 * \brief General queues
 * \author Corey Tabaka
 */

/*
   Copyright 2006 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <Queue.h>
#include <panic.h>

#define PARANOID 1

Queue::Queue(void) {
	initialize();
}

Queue::~Queue(void) {
}

void Queue::initialize(void) {
	count = 0;
	head = tail = 0;
}

QueueNode *Queue::getHead(void) {
	return head;
}

int Queue::getCount(void) {
	return count;
}

void Queue::append(QueueNode *node) {
#if PARANOID
	if (node->next || node->prev) {
		panic("%s:%d: Attempting to link an already linked node", __FILE__,
			__LINE__);
	}
#endif

	node->next = 0;
	
	if (head) {
		node->prev = tail;
		tail->next = node;
	} else {
		node->prev = 0;
		head = node;
	}
	
	tail = node;
	
	count++;
}

void Queue::prepend(QueueNode *node) {
#if PARANOID
	if (node->next || node->prev) {
		panic("%s:%d: Attempting to link an already linked node", __FILE__,
			__LINE__);
	}
#endif

	node->next = head;
	node->prev = 0;
	
	if (head) {
		head->prev = node;
	} else {
		tail = node;
	}
	
	head = node;
	
	count++;
}

void Queue::remove(QueueNode *node) {
	bool isHead = node == head;
	bool isTail = node == tail;
	
	if (isTail) {
		tail = node->prev;
		
		if (!isHead) {
			node->prev->next = 0;
		}
	}
	
	if (isHead) {
		head = node->next;
		
		if (!isTail) {
			node->next->prev = 0;
		}
	}
	
	if (!isHead && !isTail) {
		node->prev->next = node->next;
		node->next->prev = node->prev;
	}
	
	node->next = node->prev = 0;
	
	count--;
}

void Queue::insert(QueueNode *node) {
#if PARANOID
	if (node->next || node->prev) {
		panic("%s:%d: Attempting to link an already linked node", __FILE__,
			__LINE__);
	}
#endif
}

QList::QList(void) {
	next = prev = this;
}

void QList::clear(void) {
	next = prev = 0;
}

void QList::prepend(QList *node) {
#if PARANOID
	if ((node->next != 0 && node->next != node)
		|| (node->prev != 0 && node->prev != node)) {
		panic("%s:%d: Attempting to link an already linked node", __FILE__,
			__LINE__);
	}
#endif

	node->next = next;
	node->prev = this;
	next->prev = node;
	next = node;
}

void QList::append(QList *node) {
#if PARANOID
	if ((node->next != 0 && node->next != node)
		|| (node->prev != 0 && node->prev != node)) {
		panic("%s:%d: Attempting to link an already linked node", __FILE__,
			__LINE__);
	}
#endif

	node->prev = prev;
	node->next = this;
	prev->next = node;
	prev = node;
}

void QList::remove(void) {
	next->prev = prev;
	prev->next = next;
	prev = next = 0;
}

QList *QList::removeHead(void) {
	if (next != this) {
		QList *node = next;
		node->remove();
		return node;
	} else {
		return 0;
	}
}

QList *QList::removeTail(void) {
	if (prev != this) {
		QList *node = prev;
		node->remove();
		return node;
	} else {
		return 0;
	}
}

QList *QList::head(void) {
	if (next != this) {
		return next;
	} else {
		return 0;
	}
}

QList *QList::tail(void) {
	if (prev != this) {
		return prev;
	} else {
		return 0;
	}
}

QList *QList::getNext(QList *node) {
	if (node->next != this) {
		return node->next;
	} else {
		return 0;
	}
}

bool QList::isEmpty(void) {
	return next == this;
}
